package br.com.rampage.model.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import br.com.rampage.model.bean.Dependente;

public class DependenteDaoImp implements DependenteDao {

	private Session session;

	public void setSession(Session session) {
		this.session = session;
	}
	
	@Override
	public void salvar(Dependente d) {
		session.save(d);
	}

	@Override
	public void atualizar(Dependente d) {
		session.update(d);
	}

	@Override
	public void excluir(Dependente d) {
		session.delete(d);
	}

	@Override
	public Dependente carregar(Integer id) {
		return (Dependente)session.get(Dependente.class, id);
	}

	@Override
	public Dependente buscarPorId(Integer id) {
		String hql = "select d from Dependente d where d.id = :id";
		Query query = session.createQuery(hql);
		query.setInteger("id", id);
		return (Dependente) query.uniqueResult();
	}
	
	@Override
	public Dependente buscarPorIdAndName(String nome,String cpf) {
		String hql = "select d from Dependente d where d.nome = :nome and d.paciente.cpf = :cpf";
		Query query = session.createQuery(hql);
		query.setString("nome", nome);
		query.setString("cpf", cpf);
		return (Dependente) query.uniqueResult();
	}
	

	@Override
	public List<Dependente> listar() {
		return session.createCriteria(Dependente.class).list();
	}
	
}
